package com.cai.primeton.controller;

import com.cai.primeton.entity.PUser;
import com.cai.primeton.entity.R;
import com.cai.primeton.repository.UserRepo;
import com.cai.primeton.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.RequestingUserName;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @author：蔡松坡
 * @date：2021/7/1 11 : 49
 * @package：com.cai.primeton.controller
 */
@Api(tags = "用户管理")
@Controller
public class UserController {

    @Autowired
    private UserRepo userRepo;

    @Autowired
    private UserService userService;


    /**
     * 用户登录
     * @param pUser
     * @param session
     * @return
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/user/login")
    @ResponseBody
    public String login(PUser pUser, HttpSession session){
        System.out.println(pUser);
//        PUser pUser1 = userRepo.findByUsernameAndPassword(pUser.getUsername(), pUser.getPassword());
        PUser pUser1 = userService.findByUsernameAndPassword(pUser.getUsername(), pUser.getPassword());
        if(pUser1!=null){
            session.setAttribute("userSession",pUser1);
            return "success";
        }
        return "fail";
    }
    /**
     * 获取所有用户
     * @return
     */
    @ApiOperation(value = "获取所有用户信息")
    @GetMapping("/user/toUser")
    public String toUser(Model model){
        List<PUser> all1 = userService.findAll();
//        List<PUser> all = userRepo.findAll();
//        List<PUser> users = all.stream().filter(item -> item.getRoleid() != 1).collect(Collectors.toList());
        model.addAttribute("users",all1);
        return "user/userlist";
    }

    @GetMapping("/user/addUser")
    public String addUser(){

        return "user/adduser";
    }

    /**
     * 添加用户
     * @param pUser
     * @return
     */
    @ApiOperation(value = "录入用户信息")
//    @ApiImplicitParam(name = "PUser",value = "封装的用户信息")
    @PostMapping("/user/insertUser")
    @ResponseBody
    public Map insertUser(PUser pUser,HttpSession session){
        PUser puser = (PUser) session.getAttribute("userSession");
        pUser.setCreatetime(new Date());
        pUser.setCreateuser(puser.getUsername());
        userService.save(pUser);
        R r = new R(200,"success");
        return r.result();
    }

    /**
     * 根据id 删除用户
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(name = "id",value = "要删除用户的编号")
    @DeleteMapping("/user/deleteUser/{id}")
    @ResponseBody
    public Map deleteUser(@PathVariable("id") Integer id){
//        userRepo.deleteById(id);
        userService.deleteUser(id);
        R r = new R(200,"success");
        return r.result();
    }

    /**
     * 根据id 查询用户
     * @param id
     * @return
     */
    @ApiOperation(value = "查询用户信息")
    @ApiImplicitParam(name = "id",value = "用户编号")
    @GetMapping("/user/getUser/{id}")
    public String getUserById(@PathVariable("id") Integer id,Model model){
//        Optional<PUser> byId = userRepo.findById(id);
        PUser byId1 = userService.findById(id);
        model.addAttribute("user",byId1);
        return "user/userdetail";
    }

    /**
     * 修改用户
     * @param pUser
     * @return
     */
    @ApiOperation(value = "修改用户")
//    @ApiImplicitParam(name = "PUser",value = "封装的用户信息")
    @PutMapping("/user/modify")
    @ResponseBody
    public Map modifyUser(PUser pUser){
//        pUser.setModifytime(new Date());
        userService.save(pUser);
//        userRepo.save(pUser);
        R r = new R(200,"success");
        return r.result();
    }
}
